(function (aopc) {
    aopc.addExpandPage(821, function (get) {
        const Common = get('UtilsCommon', 'Common');
        const Bridge = get('UtilsCommon', 'Bridge');

        return {
            expandInit() {
                this.dialog_content_edit = this.getProperty('dialog_content_edit');
                this.titleProperty = this.getViewProperty(this.dialog_content_edit, 'title');
                this.meetingScores = this.getProperty('MeetingScores');
                this.stopTypeProperty = this.getProperty('StopType');
                this.stopTypeProperty.valueChange = this.stopTypeChange.bind(this);
                this.minutes_input_view = this.getProperty('minutes_input_view');
                this.scoreTasks = {};
            },
            stopTypeChange(value, selectData, property) {
                if (!property.isChanged) return;
                const v = parseInt(value);
                this.minutes_input_view.setIsVisible(v == 1);
            },
            getMeetingId() {
                return { pathQuery: `?$orderby=CreateDate desc&$filter=MeetingId eq @MeetingId&@MeetingId=${this.pageData.MeetingId}` };
            },
            add_score() {
                const view = this.dialog_content_edit.properties[0];
                view.entityData = null;
                this.setInitProperties(view.properties);
                this.titleProperty.text = '新增评分';
                this.dialog_content_edit.setIsVisible(true);
            },
            setInitProperties(properties) {
                properties.forEach(p => {
                    if (p.isEdit) {
                        p.value = null;
                        p.setValue = null;
                        if (p.properties) this.setInitProperties(p.properties);
                    }
                });
            },
            expandSetMeetingScoreData({ entityData }) {
                entityData.MeetingId = this.pageData.MeetingId;
                return entityData;
            },
            scoreSuccessCallback() {
                this.alertSuccess('保存成功！', () => {
                    this.dialog_content_edit.setIsVisible(false);
                    this.refreshDataList();
                });
            },
            refreshDataList() {
                this.meetingScores.id = Common.createGuid();
                this.meetingScores.isCache = false;
                this.meetingScores.refreshOptions();
            },
            getExcelImportParameter() {
                return "?entityName=MeetingScore&MeetingId=" + this.pageData.MeetingId;
            },
            uploadCallback() {
                this.refreshDataList();
            },
            exportScores() {
                Bridge.exportScores(this.meetingScores.dataSource);
            },
            subjectToScore({ property }) {
                if (property.isLoading) return;
                this.confirm('确定要议题创建评分吗？', () => {
                    const { serviceInterface } = property;
                    const { serviceName, actionName } = serviceInterface;

                    const { MeetingId } = this.pageData;

                    const payload = { pathQuery: `?MeetingId=${MeetingId}` }

                    property.isLoading = true;
                    this.dispatchAction(serviceName, actionName, payload).then(res => {
                        property.isLoading = false;
                        if (res.isSuccess !== false) {
                            this.alertSuccess('创建成功！', () => {
                                this.refreshDataList();
                            });
                        }
                    });
                });
            },
            editScore({ property }) {
                const { data } = property;
                const view = this.dialog_content_edit.properties[0];
                view.entityData = data;
                this.setInitProperties(view.properties);
                this.eventActions.entityEdit.setPropertiesValue(view.properties, data, false, true);
                this.dialog_content_edit.setIsVisible(true);
            },
            deleteScore({ property }) {
                if (property.isLoading) return;
                this.confirm('确定要删除评分吗？', () => {
                    const { serviceInterface, data } = property;
                    const { serviceName, actionName } = serviceInterface;

                    const { ScoreId, RowVersion } = data;

                    const payload = { pathQuery: `(${ScoreId})?RowVersion=${RowVersion}` }

                    property.isLoading = true;
                    this.dispatchAction(serviceName, actionName, payload).then(res => {
                        property.isLoading = false;
                        if (res.isSuccess !== false) {
                            this.alertSuccess('删除成功！', () => {
                                this.meetingScores.selectOptions = this.meetingScores.selectOptions.filter(f => f.ScoreId !== ScoreId);
                                this.refreshDataList();
                            });
                        }
                    });
                });
            },
            startScore({ property }) {
                if (property.isLoading) return;

                let list = this.getSelectDataList();
                if (list.length === 0) {
                    this.alert('请选择要开始的评分！');
                    return;
                }
                list = list.filter(f => f.Status === 0);
                if (list.length === 0) {
                    this.alert('选择的评分未有未开始的评分！');
                    return;
                }
                const isAuto = this.stopTypeProperty.getValue() === "1";

                let ms = 0;
                if (isAuto) {
                    const minutes = this.minutes_input_view.properties[0].getValue();
                    if (Common.isNullOrEmpty(minutes)) {
                        this.alert('请输入分钟数');
                        return;
                    }
                    else {
                        ms = parseInt(minutes);
                        if (!(ms > 0)) {
                            this.alert('请输入大于0的分钟数');
                            return;
                        }
                    }
                }

                const ids = list.map(m => m.ScoreId);

                this.updateScoreStatus(property, ids, 1, ms, isAuto);
            },
            endScore({ property }) {
                if (property.isLoading) return;

                let list = this.getSelectDataList();
                if (list.length === 0) {
                    this.alert('请选择要结束的评分！');
                    return;
                }
                list = list.filter(f => f.Status === 1);
                if (list.length === 0) {
                    this.alert('选择的评分状态未有进行中的评分！');
                    return;
                }

                const ids = list.map(m => m.ScoreId);

                this.updateScoreStatus(property, ids, 2);
            },
            updateScoreStatus(property, ids, status, ms, isAuto) {
                if (isAuto && status === 2) this.updateStatus(property, ids, status, ms, isAuto);
                else this.confirm(status === 1 ? '确定要开始评分吗？' : '确定要结束评分吗？', () => {
                    this.updateStatus(property, ids, status, ms, isAuto);
                });
            },
            updateStatus(property, ids, status, ms, isAuto) {
                const { serviceInterface } = property;
                const { serviceName, actionName } = serviceInterface;

                const payload = { MeetingScore: { Ids: ids, Status: status } };

                property.isLoading = true;
                this.dispatchAction(serviceName, actionName, payload).then(res => {
                    property.isLoading = false;
                    if (res.isSuccess !== false) {
                        if (isAuto && status === 2) this.refreshDataList();
                        else {
                            this.alertSuccess(status === 1 ? '评分开始成功' : '评分结束成功', () => {
                                this.refreshDataList();
                            });
                        }
                        if (status === 1 && isAuto) this.setEndScoreTask(ms, () => this.updateStatus(property, ids, 2, 0, true));

                        if (status === 1) {
                            const url = '/userScore.html?MeetingId=' + this.pageData.MeetingId;
                            this.webSocket.send('replacePage', { url });
                        }
                        else this.webSocket.send('endScore', { MeetingId: this.pageData.MeetingId });
                    }
                });
            },
            getSelectDataList() {
                return this.meetingScores.dataSource.filter(f => f.getCheckBox().getValue() === "1");
            },
            setEndScoreTask(ms, callback) {
                setTimeout(() => callback(), 1000 * 60 * ms);
            },
            scoreToBigScreen() {
                let list = this.getSelectDataList();
                if (list.length === 0) {
                    this.alert('请选择要投向大屏的评分！');
                    return;
                }
                const count = list.length;
                list = list.filter(f => f.Status === 2);
                if (list.length !== count) {
                    this.alert('选择的投票状态未有已结束的评分！');
                    return;
                }
                if (list.length === 0) {
                    this.alert('选择的评分状态未有已结束的评分！');
                    return;
                }

                const ids = list.map(m => m.ScoreId);
                this.webSocket.send('scoreToBigScreen', { ids, userId: this.visitUser.UserId });
                this.alertSuccess('评分投放到大屏消息已发出！');
            },
            closeScoreToBigScreen() {
                this.webSocket.send('closeScoreToBigScreen');
                this.alertSuccess('关闭大屏评分结果消息已发出！');
            },
            exportScoreResult({ property }) {
                if (property.isLoading) return;
                const { serviceInterface } = property;
                const { serviceName, actionName } = serviceInterface;
                const payload = { pathQuery: `&$filter=MeetingId eq @MeetingId&@MeetingId=${this.pageData.MeetingId}` }

                property.isLoading = true;
                this.dispatchAction(serviceName, actionName, payload).then(res => {
                    property.isLoading = false;
                    if (res.isSuccess !== false) {
                        if (res.length === 0) {
                            this.alert('当前会议无评分记录！');
                            return;
                        }
                        Bridge.exportScoreResult(res);
                    }
                });
            },
            setRowData({ p, rowData }) {
                if (p.name === 'edit' || p.name === 'split_line') {
                    p.isVisible = rowData.Status === 0;
                }
            }
        }
    });
})(window.aopc);